package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/27 9:17
 * @Version 1.0
 */
@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //  初始化交换机队列路由键的绑定关系！
    @Bean
    public DirectExchange exchange(){
        //  创建交换机
        return new DirectExchange(exchange_dead,true,false);
    }

    //  初始化交换机队列路由键的绑定关系！
    @Bean
    public Queue queue1(){
        //  创建map
        HashMap<String, Object> hashMap = new HashMap<>();
        //  消息的过期时间
        hashMap.put("x-message-ttl",10000);
        hashMap.put("x-dead-letter-exchange",exchange_dead);
        hashMap.put("x-dead-letter-routing-key",routing_dead_2);
        //  创建交换机
        return new Queue(queue_dead_1,true,false,false,hashMap);
    }

    //  设置队列1的绑定关系
    @Bean
    public Binding binding1(){
        //  返回绑定关系
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }

    //  声明一个队列2
    @Bean
    public Queue queue2(){

        return new Queue(queue_dead_2,true,false,false);
    }

    //  设置队列2的绑定关系
    @Bean
    public Binding binding2(){
        //  返回绑定关系
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }

}
